﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp8
{
    public partial class Editform : Form
    {
        public Editform()
        {
            InitializeComponent();
        }
        public Editform (int id,string title,string author)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = title;
            textBox2.Text = author;
  
        }

        private int id;
        /// <summary>
        /// 确认保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;
            var author = textBox2.Text;
          
            var ass = !string.IsNullOrEmpty(author);
            if (id > 0)
            {
                if (ass)
                {
                    var sql = string.Format("updata Articles set title='{0}',author='{2}',where id={3}", title, author, id);

                    var res = DbHeiper.AddOrUpdaterOrDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新失败！", "提示");
                        this.Close();
                    }
                }
            }
            else
            {
                if (ass)
                {
                    var sql = string.Format("updata Articles set title='{0}',author='{1}',where id={2}", title, author, id);

                    var res = DbHeiper.AddOrUpdaterOrDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败！", "提示");
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("文章作者不能为空", "提示");
                }
            }
                  
            }
               
        }

    private void Button2_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Yes;
        this.Close();
    }
}
